<template>
  <el-aside width='width'>
    <el-menu
        background-color="#545c64"
        text-color="#fff"
        :collapse = "isCollapse"
    >
      <h3 v-show="!isCollapse">通用后台管理</h3>
      <el-menu-item
          v-for="item in noChild" :index="item.path" :key="item.path"
      >
        <component class="icons" :is="item.icon"></component>
        <span>{{ item.label }}</span>
      </el-menu-item>
      <el-sub-menu v-for="item in hasChild" :index="item.path" :key="item.path">
        <template #title>
          <component class="icons" :is="item.icon"></component>
          <span>{{ item.label }}</span>
        </template>
        <el-menu-item-group>
          <el-menu-item
              v-for="(subItem) in item.children"
              :index="subItem.path"
              :key="subItem.path">
            <component class="icons" :is="subItem.icon"></component>
            <span>{{ subItem.label }}</span>
          </el-menu-item>
        </el-menu-item-group>
      </el-sub-menu>
    </el-menu>
  </el-aside>
</template>


<script setup lang='ts'>
import {ref, computed} from 'vue'
import { useAllDataStore } from '../stores'
const list = ref([
  {
    path: '/home',
    name: 'home',
    label: '首页',
    icon: 'house',
    url: 'Home'
  },
  {
    path: '/mall',
    name: 'mall',
    label: '商品管理',
    icon: 'video-play',
    url: 'Mall'
  },
  {
    path: '/user',
    name: 'user',
    label: '用户管理',
    icon: 'user',
    url: 'User'
  },
  {
    path: 'other',
    label: '其他',
    icon: 'location',
    children: [
      {
        path: '/page1',
        name: 'page1',
        label: '页面1',
        icon: 'setting',
        url: 'page1'
      },
      {
        path: '/page2',
        name: 'page2',
        label: '页面2',
        icon: 'setting',
        url: 'page2'
      },
      {
        path: '/page3',
        name: 'page3',
        label: '页面3',
        icon: 'setting',
        url: 'page3'
      },
    ]
  },
])

const noChild = computed(() => list.value.filter(item => !item.children))
const hasChild = computed(() => list.value.filter(item => item.children))

const store = useAllDataStore()
const isCollapse = computed(() => store.state.isCollapse)
const width = computed(() => isCollapse.value ? '64px' : '180px')

</script>


<style lang="less" scoped>
.icons {
  width: 18px;
  height: 18px;
  margin-right: 5px;
}

.el-menu {
  //移除元素的右侧边框
  border-right: none;

  h3 {
    //文字距离顶部高度
    line-height: 48px;
    color: #fff;
    //文字居中
    text-align: center;
  }
}

.el-aside {
  height: 100% !important; /* 强制继承父级高度 */
  background-color: #545c64;
}

</style>
